<?php

error_reporting(E_ALL);
ini_set('display_errors', 1);

require_once('admin.php');

$title = 'Rewrite Rules';
$this_file = 'lightpress/wp-plugin/mod_rewrite.php';
$parent_file = 'lightpress/wp-plugin/main.php';

$submenu[$this_file][] = array(
    'LightPress',
    6,
    'lightpress/wp-plugin/main.php',
    'LightPress'
    );

require_once 'LightPressModRewrite.php';
$lightpress_rewrite_helper =& new LightPressModRewrite();

$lp_install_type = get_option('lp_opt_install_type');

$noheader = isset($_GET['noheader']);

if ($noheader) {
    header("Content-Type: text/plain");
    header('Content-Disposition: attachment; filename=.htaccess');
    echo "# BEGIN WordPress\n\n" . $lightpress_rewrite_helper->buildHtaccess() . "\n# END WordPress";
    exit();
}
?>

<style type="text/css">
h3 { margin: 0px; margin-top: 12px; margin-bottom: 6px; }
caption { font-weight: bold; text-align: center; }
label { font-weight: bold; }
small { font-size: 0.9em; font-style: italic; color: #666; }
pre { border: solid 1px #999; }
code { color: #333366; font-style: italic; }
code.opt { font-weight: bold; font-style: normal; }
.error { color: #993333; }
.warning { color: #cc33cc; }
</style>

<div class="wrap"> 
<h2>LightPress Rewrite Rules</h2>
<p>
<?php

$home_path = get_home_path();
$install_type = get_option('lp_opt_install_type');
if ($install_type == 'testing')
    $home_path .= substr(get_option('lp_opt_url'), strlen(get_option('home')) + 1);
if (substr($home_path, -1) != DIRECTORY_SEPARATOR)
    $home_path = substr($home_path, 0, -1) . DIRECTORY_SEPARATOR;

if (!get_option('lp_opt_post_prefix')) {
    // not using mod_rewrite
    echo "LightPress is set to use 'dumb' instead of
    '<a href=\"http://codex.wordpress.org/Introduction_to_Blogging#Pretty_Permalinks\">pretty</a>' permalinks,
    so you have no need to set up rules for Apache's mod_rewrite. If you think you need to set an htaccess file anyway, you can ";
} else {
    // using mod_rewrite
    echo "Lightpress is set to use '<a href=\"http://codex.wordpress.org/Introduction_to_Blogging#Pretty_Permalinks\">pretty</a>'
    permalinks";
    // find out the correct .htaccess location
    $htaccess = "$home_path.htaccess";
    if (file_exists($htaccess)) {
        echo ", your htaccess file exists";
        if (is_writable($htaccess)) {
            echo ", is writable, ";
            if ($wp_rewrite->using_mod_rewrite_permalinks())
                echo " and WordPress knows about it, so everything should be set up for you.<br />If you still want to set it manually, you can ";
            else
                echo " but WordPress knows nothing about it, so you need to update it by hand.<br /> To do so, ";
        } else {
            " but it's not writable, ";
            if ($wp_rewrite->using_mod_rewrite_permalinks())
                echo " and WordPress know nothing about it, so you need to update it by hand.<br /> To do so, ";
        }
    } else {
        echo ", but your htaccess file does not seem to exist";
            if ($wp_rewrite->using_mod_rewrite_permalinks())
                echo " and WordPress knows nothing about it";
        echo ", so you need to update it by hand.<br /> To do so, ";
    }
}
?>
copy the following lines and paste them into a file named '<code>.htaccess</code>' in <code><?php echo $home_path; ?></code>.
You can also <a href="<?php echo $_SERVER['REQUEST_URI']; ?>&noheader=1">download</a> a ready-made <code>.htaccess</code> file and save it to the same folder.
</p>
<pre class="conf">
<?php
echo "# BEGIN WordPress\n\n" . htmlentities($lightpress_rewrite_helper->buildHtaccess()) . "\n# END WordPress";
?>
</pre></code>
</div>
